/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-02-02
 * Time: 15:34
 *
 * https://www.nowcoder.com/share/jump/7745334641706859288143
 */
public class BM94 {

    public long maxWater(int[] arr) {
        if (arr == null || arr.length < 3) {
            return 0;
        }
        // 左右双指针
        int left = 0, right = arr.length - 1;
        // 边界高度
        int maxL = 0, maxR = 0;
        // 容量
        int capacity = 0;
        while (left < right) {
            // 维护边界的最大值
            maxL = Math.max(maxL, arr[left]);
            maxR = Math.max(maxR, arr[right]);
            // 较短的边界决定该列储存的水
            if (maxL < maxR) {
                capacity += maxL - arr[left++];
            } else {
                capacity += maxR - arr[right--];
            }
        }
        return capacity;
    }
}
